#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"
#include"malloc.h"
void listprintf(sl* p)
{
	sl* q = p;
	while (q->next != NULL)
	{
		printf("%d->", q->data);
		q = q->next;
	}
	printf("%d->NULL\n", q->data);
}
void pushback(sl** p, Datay x)
{
	sl* new = (sl*)malloc(sizeof(sl));
	new->data = x;
	new->next = NULL;
	if (*p == NULL)
	{
		*p = new;
	}
	else
	{
		sl* w =*p;
		while (w->next != NULL)
		{
			w = w->next;
		}
		w->next = new;
	}
}
void pushfront(sl** p, Datay x)
{
	sl* new = (sl*)malloc(sizeof(sl));
	new->data = x;
	new->next = *p;
		*p = new;
}

sl* listfind(sl* p, Datay x)
{
	sl* w = p;
	while (w)
	{
		if (w->data == x)
		{
			return w;
		}
		else
		{
			w = w->next;
		}
	}
	return NULL;
}

void listinsert(sl** p, sl* x, Datay y)
{
	sl* w = (sl*)malloc(sizeof(sl));
	w->data = y;
	w->next = NULL;
	sl* t = *p;
	if (*p==x)
	{
		w->next = *p;
		*p = w;
	}
	else 
	{
		while (t->next != x)
		{
			t = t->next;
		}
			t->next = w;
			w->next = x;
		
	}
}

void detellist(sl** p, Datay x)
{
	sl* t = NULL;
	sl* r = *p;
	while (r)
	{
		if (r->data == x)
		{
			if (r == *p)
			{
				sl* w = (*p)->next;
				free(*p);
				*p = w;
			}
			else
			{
				t->next = r->next;
				free(r);
				r = t->next;
			}
		}
		else
		{
			t = r;
			r = r->next;

		}
	}
}

